/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.edu.agh.student.logic.util;

import javax.ejb.Stateless;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import pl.edu.agh.student.logic.service.ApplicationServiceImpl;
import pl.edu.agh.student.model.Application;
import pl.edu.agh.student.model.State;

/**
 *
 * @author janburkot
 */
@Stateless
@Name("notificationService")
public class NotificationServiceImpl implements NotificationService {

    @In(value = "applicationService", create = true, required = false)
    @Out
    protected ApplicationServiceImpl applicationService;
    
    private void notify(Application application, NotificationType type) {
        if(NotificationType.ACCEPTED.equals(type)){
            application.setState(State.ACCEPTED);
        }
        
        if(NotificationType.REJECTED.equals(type)){
            application.setState(State.REJECTED);
        }
        
        applicationService.saveOrUpdate(application);

    }

    public void notifyAccepted(Object execution) {
        if(execution instanceof Long){
            Long id = (Long)execution;
            Application application = applicationService.find(id);
            notify(application, NotificationType.ACCEPTED);
        }
        System.out.println("call notifyAccepted(Object execution)");
        System.out.println(execution);
        System.out.println(execution.getClass());
    }

    public void notifyRejected(Object execution) {
        if(execution instanceof Long){
            Long id = (Long)execution;
            Application application = applicationService.find(id);
            notify(application, NotificationType.REJECTED);
        }
        System.out.println("call notifyAccepted(Object execution)");
        System.out.println(execution);
        System.out.println(execution.getClass());
    }
    
}
